package fit.deni.pages;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.BeanEditForm;
import org.apache.tapestry5.corelib.components.EventLink;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Hibernate;

import fit.deni.beans.DVD;
import fit.deni.beans.Glumac;
import fit.deni.beans.Reditelj;
import fit.deni.beans.Zanr;
import fit.deni.beans.Zaposleni;
import fit.deni.hibernate.interfaces.DVDService;
import fit.deni.hibernate.interfaces.GlumacService;
import fit.deni.hibernate.interfaces.RediteljService;
import fit.deni.hibernate.interfaces.ZanrService;

public class Movies {

	Logger log = Logger.getLogger(Movies.class);

	@Persist
	private Collection<DVD> selectedDVDs;
	@Property
	private DVD singleDVD;
	@Property
	@Persist(PersistenceConstants.FLASH)
	private DVD newDVD;
	@Persist
	private String query;
	@Property
	private Collection<Zanr> selectedGenres;
	@Property
	private Collection<Reditelj> selectedDirectors;
	@Property
	private Collection<Glumac> selectedActors;

	@Property
	@SessionState
	private Zaposleni korisnik;
	private boolean korisnikExists;

	@Component
	private EventLink edit, delete;

	@Inject
	private DVDService dvdService;
	@Inject
	private ZanrService zanrService;
	@Inject
	private GlumacService glumacService;
	@Inject
	private RediteljService rediteljService;

	@InjectPage
	private Movie movie;

	@Component
	private BeanEditForm addDVDForm;

	public void setSelectedDVDs(Collection<DVD> dvds) {
		selectedDVDs = dvds;
	}

	public Collection<DVD> getSelectedDVDs() {
		return selectedDVDs;
	}

	private Object onActivate() {
		if (!korisnikExists)
			return Login.class;
		return null;
	}

	public Object onDelete(String id) {
		try {
			DVD toRemove = dvdService.getDVDById(Integer.parseInt(id));
			dvdService.deleteDVD(toRemove);
			selectedDVDs = dvdService.getAll();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Here you should handle delete errors");
		}
		return this;
	}

	public void onEdit(String id) {
		try {
			newDVD = dvdService.getDVDById(Integer.parseInt(id));
			selectedGenres = newDVD.getZanrovi();
			selectedActors = newDVD.getGlumci();
//			selectedDirectors = newDVD.getReditelji();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Here you should handle edit errors");
		}
	}
	
	private Object onSubmitFromAddDVDForm() {
		if (dvdService.getDVDbyTitle(newDVD.getOriginalniNaziv()) != null) {
			addDVDForm.recordError("Film sa nazivom "
					+ newDVD.getOriginalniNaziv() + " ili prevodom "
					+ newDVD.getPrevod() + " vec postoji, odaberite drugi\n");
		}
		if (selectedGenres==null || selectedGenres.size() < 1) {
			addDVDForm.recordError("Morate unijeti makar jedan zanr");
		}
		if (selectedActors==null || selectedActors.size() < 1) {
			addDVDForm.recordError("Morate odabrati makar jednog glumca");
		}
//		if (selectedDirectors==null || selectedDirectors.size() < 1) {
//			addDVDForm.recordError("Morate odabrati makar jednog rezisera");
//		}
		if(addDVDForm.isValid()) {
			newDVD.setZanrovi(selectedGenres);
			newDVD.setGlumci(selectedActors);
//			newDVD.setReditelji(selectedDirectors);
			dvdService.addDVD(newDVD);
			newDVD = new DVD();
			selectedGenres = new LinkedList<Zanr>();
			selectedActors = new LinkedList<Glumac>();
			selectedDirectors = new LinkedList<Reditelj>();
			selectedDVDs = dvdService.getNewDVDs();
		}
		return this;
	}

	private Object onSelect(String id) {
		DVD selected = dvdService.getDVDById(Integer.parseInt(id));
		Hibernate.initialize(selected);
		movie.setDvdObject(selected);
		return movie;
	}

	public List<Zanr> getAllGenres() {
		return new LinkedList<Zanr>(zanrService.getAll());
	}
	public List<Reditelj> getAllDirectors() {
		return new LinkedList<Reditelj>(rediteljService.getAll());
	}
	public List<Glumac> getAllActors() {
		return new LinkedList<Glumac>(glumacService.getAll());
	}
//	@Property
//	private ValueEncoder<Reditelj> encoderDirector = new ValueEncoder<Reditelj>() {
//		public String toClient(Reditelj value) {
//			return value.getIme();
//		}
//		public Reditelj toValue(String clientValue) {
//			return rediteljService.getDirectorByName(clientValue);
//		}
//	};
	@Property
	private ValueEncoder<Glumac> encoderActor = new ValueEncoder<Glumac>() {
		public String toClient(Glumac value) {
			return value.getIme();
		}
		public Glumac toValue(String clientValue) {
			return glumacService.getActorByName(clientValue);
		}
	};
	@Property
	private ValueEncoder<Zanr> encoderGenre = new ValueEncoder<Zanr>() {
		public String toClient(Zanr value) {
			return value.getTip();
		}
		public Zanr toValue(String clientValue) {
			return zanrService.getGenreByName(clientValue);
		}
	};

}
